package com.ruoyi.web.controller.common;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class WeChatUtils {

    private static final Logger log = LoggerFactory.getLogger(WeChatUtils.class);

    public String getAccessToken(String appId, String secret){
        String url = "https://api.weixin.qq.com/cgi-bin/token?appid=" + appId + "&secret=" + secret + "&grant_type=client_credential";
        String tokenResult = HttpUtils.sendGet(url);
        String accessToken = JSONObject.parseObject(tokenResult).getString("access_token");
        return accessToken;
    }

    /**
     * 下载带参数的小程序二维码
     * */
    public String downloadMiniCode(String appId, String secret, String page, String scene, String name){
        String accessToken = getAccessToken(appId,secret);
        try {
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("page",page);
            paramMap.put("scene",scene);
//            paramMap.put("is_hyaline",true);
            paramMap.put("width", 430);//不是必须，需要的宽度，默认430x430，最小280最大1280
            paramMap.put("check_path", false);//不是必须，需要的宽度，默认430x430，最小280最大1280

            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            printWriter.write(JSON.toJSONString(paramMap));
            // flush输出流的缓冲
            printWriter.flush();

            InputStream ins = httpURLConnection.getInputStream();
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = ins.read(buffer))) {
                output.write(buffer, 0, n);
            }
            byte[] pdfByteArray = output.toByteArray();

            return Base64.getEncoder().encodeToString(pdfByteArray);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
